package edu.ynu.se.xiecheng.achitectureclass.entity;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import edu.ynu.se.xiecheng.achitectureclass.common.entity.LogicEntity;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.Set;

@Entity
@Getter
@Setter
@Where(clause = "is_deleted = 0")
public class Shop extends LogicEntity {
    @Column
    private String shopName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "business_id")
    @JsonIgnoreProperties("shops")
    private Business business;

    @OneToMany(mappedBy = "shop",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties("shop")
    private Set<ShopItem> shopItems;

    @OneToMany(mappedBy = "shop",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    @JsonIgnore
//    @JsonIgnoreProperties("shop")
    private Set<LineItem> lineItems;
    @JsonIgnore
    public ShopItem createShopItem(Item item, Double price) {
        ShopItem shopItem = item.findShopItem(this);//去重
        if (shopItem != null) {
            return shopItem;
        }
        shopItem = new ShopItem();
        shopItem.setShop(this);
        shopItem.setItem(item);
        shopItem.setShopItemName(item.getItemName());
        shopItem.setPrice(price);
        shopItem.setShelfStatus(0);
        shopItems.add(shopItem);
        return shopItem;
    }
    public ShopItem changeToOnShelfStatus(ShopItem shopItem){
        shopItem.setShelfStatus(1);
        return shopItem;
    }
    public ShopItem changeToOffShelfStatus(ShopItem shopItem){
        shopItem.setShelfStatus(0);
        return shopItem;
    }

    public ShopItem changePrice(ShopItem shopItem, Double price){
        shopItem.setPrice(price);
        return shopItem;
    }

}
